﻿using Rabbit.Domain;
using Rabbit.Domain.Slaughter;
using Rabbit.DTO.Input;
using Rabbit.DTO.Output;
using Rabbit.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Rabbit.Service
{
    public class TraceManageService
    {

        /// <summary>
        /// 屠宰信息
        /// </summary>
        RabbitRepository<SlaughterInfo> _sires;
        RabbitRepository<TraceManage> _tres;

        public TraceManageService(RabbitRepository<TraceManage> tres, RabbitRepository<SlaughterInfo> sires)
        {
            _tres = tres;
            _sires = sires;
        }

        /// <summary>
        /// 商品显示
        /// </summary>
        /// <returns></returns>
        public List<TraceManage> GetTraceManages()
        {
            return _tres.GetAll().Where(x=>x.IsDel!=1).ToList();
        }

        /// <summary>
        /// 添加商品
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public int AddTraceManage(TraceManageInput input)
        {
            var list=new TraceManage();
            list.TMName= input.TMName;
            list.AddName= input.AddName;
            list.UpdName= input.UpdName;
            list.TMBatch= input.TMBatch;
            list.TMManufacturing= input.TMManufacturing;
            list.TMProduction= input.TMProduction;
            list.IsDel = 0;
            list.AddDate = DateTime.Now;
            list.TMImage= input.TMImage;
            list.TMExpiration= input.TMExpiration;
            list.TMNumber = 0;
            return _tres.Add(list);
        }
        /// <summary>
        /// 逻辑删
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="names"></param>
        /// <returns></returns>
        public int DelT(string ids, string names)
        {
            var id = ids.Split(',');
            int[] ints = id.Select(x => int.Parse(x.Trim())).ToArray();
            var list = _tres.GetAll().Where(x => ints.Contains(x.TMid)).ToList();
            foreach (var item in list)
            {
                item.IsDel = 1;
                item.DelName = names;
                item.DelDate = DateTime.Now;
            }
            return _tres.Update(list);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int UpdT(TraceManage entity)
        {
            var list= _tres.GetAll().Where(x=>x.TMid==entity.TMid).ToList();
            foreach (var item in list)
            {
                item.AddName = entity.AddName;
                item.AddDate= entity.AddDate;
                item.UpdDate = DateTime.Now;
                item.UpdName = entity.UpdName;
                item.TMBatch = entity.TMBatch;
                item.TMManufacturing = entity.TMManufacturing;
                item.TMExpiration = entity.TMExpiration;
                item.TMid = entity.TMid;
                item.TMName = entity.TMName;
                item.TMImage = entity.TMImage;
                item.TMProduction = entity.TMProduction;
                item.DelDate= null;
                item.DelName =null;
                item.IsDel=entity.IsDel; 
                var lis = _sires.GetAll().FirstOrDefault(x => x.TMid == entity.TMid);
                if (lis != null)
                {
                    if (entity.TMBatch != lis.TMBatch)
                    {
                        lis.TMBatch = entity.TMBatch;
                        _sires.Updates(lis);
                    }
                }

            }
            return _tres.Update(list);
        }
        /// <summary>
        /// 修改导出数量
        /// </summary>
        /// <param name="number"></param>
        /// <param name="tMid"></param>
        /// <returns></returns>
        public int GetDc(int number,int tMid)
        {
            var list = _tres.GetAll().Where(x => x.TMid == tMid).ToList();
            foreach (var item in list)
            {
                item.TMNumber= number;
            }
            return _tres.Update(list);
        }
        
        /// <summary>
        /// 查看详情
        /// </summary>
        /// <param name="tmid"></param>
        /// <returns></returns>
        public TraceManageOutput TraceManageInfo(int tmid)
        {
            var list= _tres.GetAll().FirstOrDefault(x => x.TMid == tmid);
            var data=new TraceManageOutput();
            data.TMBatch = list.TMBatch;
            data.TMManufacturing= list.TMManufacturing;
            data.TMExpiration= list.TMExpiration;
            data.TMName= list.TMName;
            data.TMImage= list.TMImage;
            data.TMProduction= list.TMProduction;
            return data;
        }




    }
}
